package ext.generic.doc.processors;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Vector;

import wt.doc.WTDocument;
import wt.fc.collections.WTArrayList;
import wt.identity.DisplayIdentity;
import wt.identity.IdentityFactory;
import wt.method.RemoteAccess;
import wt.session.SessionHelper;
import wt.util.WTException;
import wt.util.WTMessage;
import wt.workflow.engine.ProcessData;
import wt.workflow.engine.WfVariable;
import wt.workflow.work.WfAssignedActivity;
import wt.workflow.work.WorkItem;

import com.ptc.core.components.forms.DefaultObjectFormProcessor;
import com.ptc.core.components.forms.FormProcessingStatus;
import com.ptc.core.components.forms.FormResult;
import com.ptc.core.components.util.FeedbackMessage;
import com.ptc.core.ui.resources.FeedbackType;
import com.ptc.netmarkets.util.beans.NmCommandBean;
import com.ptc.netmarkets.util.misc.NmContext;

import ext.generic.doc.DocWorkflowUtil;

/**
 * 文档流程中添加随签对象的类,响应jsp页面的action
 */
public class AddReviewDocFormProcessor extends DefaultObjectFormProcessor implements RemoteAccess, Serializable
{

    public AddReviewDocFormProcessor()
    {
    }

    /**
     * 文档流程中添加随签对象的主要方法
     * @param nmcommandbean
     * @param list
     * @return
     * @throws WTException
     */
    public FormResult postProcess(NmCommandBean nmcommandbean, List list) throws WTException
    {
        WTArrayList reviewdoc = new WTArrayList();
        ArrayList existvec = new ArrayList();
        ArrayList errorstatevec = new ArrayList();
        ArrayList partdoc = new ArrayList();
        java.util.Locale locale = SessionHelper.getLocale();
        ArrayList arraylist = nmcommandbean.getSelected();
        WorkItem workitem = (WorkItem) nmcommandbean.getPrimaryOid().getRef();
        WfAssignedActivity wfassignedactivity = (WfAssignedActivity) workitem.getSource().getObject();
        ProcessData context = wfassignedactivity.getContext();
        WfVariable[] array=context.getVariableList();
         for(int i=0;i<array.length;i++)
        {
        	System.out.println("array["+i+"]="+array[i].getName()+array[i].getValue());
        }
        reviewdoc = (WTArrayList) (context.getValue("reviewObjs"));
        WTDocument doc=(WTDocument)reviewdoc.getPersistable(0);
        //WTDocument doc = (WTDocument) reviewdoc.get(0); 
        String docmodifer = doc.getModifierName();
        if (arraylist != null && arraylist.size() > 0)
        {
            for (int i = 0; i < arraylist.size(); i++)
            {
                NmContext nmcontext = (NmContext) arraylist.get(i);
                System.out.println(">>>>>>>>>>>>>>>"+nmcontext.getTargetOid().getWtRef());
                WTDocument wtdocument = (WTDocument) nmcontext.getTargetOid().getWtRef().getObject();
                String state = wtdocument.getLifeCycleState().toString();
                String modifername = wtdocument.getModifierName();
                boolean ispartdoc = DocWorkflowUtil.isReviewDoc(wtdocument, "随签", "doc");
                if (ispartdoc)
                {
                    partdoc.add(wtdocument);
                }
                else if (reviewdoc.contains(wtdocument))
                {
                    existvec.add(wtdocument);
                }
                else if ((state.equalsIgnoreCase("INWORK")) & (!modifername.equalsIgnoreCase(docmodifer)))
                {
                    errorstatevec.add(wtdocument);
                }
                else
                {
                    reviewdoc.add(wtdocument);
                }
            }
        }
        FormResult formresult = new FormResult();
        if (existvec.size() > 0 || errorstatevec.size() > 0 || partdoc.size() > 0)
        {
            formresult.addFeedbackMessage(getAddFbMsg(FeedbackType.FAILURE, existvec, errorstatevec, partdoc, locale));
            formresult.setStatus(FormProcessingStatus.FAILURE);
        }
        else if (reviewdoc.size() != 0)
        {
            context.setValue("reviewObjs", reviewdoc);
            wfassignedactivity.setContext(context);
            wt.fc.PersistenceHelper.manager.save(wfassignedactivity);
            formresult.setStatus(FormProcessingStatus.SUCCESS);
            super.postProcess(nmcommandbean, list);
        }
        return formresult;
    }

    public static FeedbackMessage getAddFbMsg(FeedbackType feedbacktype, List list1, List list2, List list3, Locale locale) throws WTException
    {
        String s = WTMessage.getLocalizedMessage("ext.generic.doc.docResource", "ADD_ACTION_REVIEWDOC_FAILED", null);
        String s1 = WTMessage.getLocalizedMessage("ext.generic.doc.docResource", "ADD_ACTION_REVIEWDOC_FAILED_MSG1", null);
        String s2 = WTMessage.getLocalizedMessage("ext.generic.doc.docResource", "ADD_ACTION_REVIEWDOC_FAILED_MSG2", null);
        String s3 = WTMessage.getLocalizedMessage("ext.generic.doc.docResource", "ADD_ACTION_REVIEWDOC_FAILED_MSG3", null);
        return createFeedbackMessage(feedbacktype, locale, s, s1, s2, s3, list1, list2, list3);
    }

    public static FeedbackMessage createFeedbackMessage(FeedbackType feedbacktype, Locale locale, String s, String s1, String s2, String s3, List list1, List list2,
                                                        List list3) throws WTException
    {
        StringBuffer stringbuffer = new StringBuffer();
        if (list1.size() > 0)
        {
            stringbuffer.append(s1);
            stringbuffer.append("\n");
            for (int i = 0; i < list1.size(); i++)
            {
                Object obj1 = list1.get(i);
                DisplayIdentity displayidentity1 = IdentityFactory.getDisplayIdentity(obj1);
                stringbuffer.append("\n   ");
                stringbuffer.append(displayidentity1.getLocalizedMessage(locale));
            }
            stringbuffer.append("\n\n");
        }
        if (list2.size() > 0)
        {
            stringbuffer.append(s2);
            stringbuffer.append("\n");
            for (int j = 0; j < list2.size(); j++)
            {
                Object obj2 = list2.get(j);
                DisplayIdentity displayidentity2 = IdentityFactory.getDisplayIdentity(obj2);
                stringbuffer.append("\n   ");
                stringbuffer.append(displayidentity2.getLocalizedMessage(locale));
            }
            stringbuffer.append("\n\n");
        }
        if (list3.size() > 0)
        {
            stringbuffer.append(s3);
            stringbuffer.append("\n");
            for (int k = 0; k < list3.size(); k++)
            {
                Object obj3 = list3.get(k);
                DisplayIdentity displayidentity3 = IdentityFactory.getDisplayIdentity(obj3);
                stringbuffer.append("\n   ");
                stringbuffer.append(displayidentity3.getLocalizedMessage(locale));
            }
        }
        FeedbackMessage feedbackmessage = new FeedbackMessage(feedbacktype, locale, s, null, new String[]
                { stringbuffer.toString() });
        return feedbackmessage;
    }
}
